package com.aiju.widgit.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.aiju.base.R;
import com.aiju.base.dialog.BaseDialog;
import com.aiju.common.PixValue;

/**
 * @author: 佐为
 * @date: 2016-11-17 14:52
 * @version: V1.0
 * @Description: 描述
 */
public class SimpleDialog extends BaseDialog implements View.OnClickListener {

    private TextView titleTextView;
    private TextView leftTextView;
    private TextView rightTextView;

    private View view;

    public SimpleDialog(Context context) {
        super(context);
    }

    @Override
    protected int getDialogStyleId() {
        return R.style.dialog;
    }

    @Override
    protected View getView() {
        View inflate = mLayoutInflater.inflate(R.layout.dialog_simple, null);
        titleTextView = (TextView) inflate.findViewById(R.id.tv_dialog_title);
        leftTextView = (TextView) inflate.findViewById(R.id.tv_dialog_left);
        rightTextView = (TextView) inflate.findViewById(R.id.tv_dialog_right);
        view = inflate.findViewById(R.id.view_dialog_lin);
        leftTextView.setOnClickListener(this);
        rightTextView.setOnClickListener(this);
        return inflate;
    }

    /**
     * 设置对话框LayoutParams
     *
     * @return
     */
    public SimpleDialog setLayoutParams() {
        WindowManager.LayoutParams lp = getDialogWindow().getAttributes();
        lp.width = PixValue.dp.valueOf(317);
        lp.height = PixValue.dp.valueOf(165);
        getDialogWindow().setGravity(Gravity.CENTER);
        getDialogWindow().setAttributes(lp);
        return this;
    }

    public SimpleDialog setTitileText(String titile) {
        this.titleTextView.setText(titile);
        return this;
    }

    public SimpleDialog setLeftText(String leftText) {
        if (TextUtils.isEmpty(leftText)) {
            leftTextView.setVisibility(View.GONE);
            view.setVisibility(View.GONE);
        }
        this.leftTextView.setText(leftText);
        return this;
    }

    public SimpleDialog setRightText(String rightText) {
        if (TextUtils.isEmpty(rightText)) {
            rightTextView.setVisibility(View.GONE);
            view.setVisibility(View.GONE);
        }
        this.rightTextView.setText(rightText);
        return this;
    }

    private OnSimpleClick mOnSimpleClick;

    public SimpleDialog setOnSimpleClick(OnSimpleClick onSimpleClick) {
        this.mOnSimpleClick = onSimpleClick;
        return this;
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.tv_dialog_left) {
            if (mOnSimpleClick != null) {
                mOnSimpleClick.onCancel();
                dismiss();
            }
        } else if (i == R.id.tv_dialog_right) {
            if (mOnSimpleClick != null) {
                mOnSimpleClick.onSubmit();
                dismiss();
            }
        }
    }

    public interface OnSimpleClick {
        void onCancel();

        void onSubmit();
    }


}
